package com.xyb.common.config;

import com.xyb.dao.*;
import com.xyb.entity.*;
import com.xyb.utils.Base64Utils;
import com.xyb.utils.IdUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 系统初始化
 * delete from sys_user where user_id = 0;
 * delete from sys_user_role where user_role_id = 0;
 * delete from sys_role where role_id = 0;
 * delete from sys_menu_role where role_id = 0;
 *
 * select * from sys_user;
 * select * from sys_user_role;
 * select * from sys_role;
 * select * from sys_menu_role;
 */
@Component
public class AdminConfig implements InitializingBean {
    @Resource
    private SysUserDao sysUserDao;
    @Resource
    private SysRoleDao sysRoleDao;
    @Resource
    private SysUserRoleDao sysUserRoleDao;
    @Resource
    private SysMenuDao sysMenuDao;
    @Resource
    private SysMenuRoleDao sysMenuRoleDao;
    @Resource
    private IdUtils idUtils;
    @Resource
    private PasswordEncoder passwordEncoder;

    @Override
    public void afterPropertiesSet() throws Exception {
        //系統初始化 用户
        SysUser admin = sysUserDao.selectSysUserByUserId(0L);
        if (admin == null) {
            SysUser sysUser = SysUser.builder()
                    .userId(0L)
                    .userName("admin")
                    .userNickName("超级管理员")
                    .iphoneNo("15251922665")
                    .userPwd(passwordEncoder.encode(Base64Utils.base64Encode("123456")))
                    .headPortraitUrl("https://img0.baidu.com/it/u=2809737200,31955359&fm=26&fmt=auto&gp=0.jpg")
                    .status(1)
                    .showOrder(0L)
                    .createTime(new Date())
                    .build();
            sysUserDao.insertSysUser(sysUser);
        }
        //系統初始化 角色
        SysRole adminRole = sysRoleDao.selectSysRoleByRoleId(0L);
        if (adminRole == null) {
            SysRole sysRole = SysRole.builder()
                    .roleId(0L)
                    .roleName("admin")
                    .roleDesc("超级管理员")
                    .showOrder(0L)
                    .status(1)
                    .build();
            sysRoleDao.insertSysRole(sysRole);
        }
        //分配角色
        SysUserRole adminUserRole = sysUserRoleDao.selectSysUserRoleByUserRoleId(0L);
        if (adminUserRole == null) {
            SysUserRole sysUserRole = SysUserRole.builder()
                    .userRoleId(0L)
                    .userId(0L)
                    .roleId(0L)
                    .build();
            sysUserRoleDao.insertSysUserRole(sysUserRole);
        }
        //分配菜单
        List<SysMenu> menuList = sysMenuDao.selectSysMenuList();
        if(menuList!=null){
            sysMenuRoleDao.deleteSysMenuByRoleId(0L);
            List<SysMenuRole> list = new ArrayList<>();
            menuList.forEach(i->{
                SysMenuRole sysMenuRole = SysMenuRole.builder()
                        .menuRoleId(idUtils.nextId())
                        .menuId(i.getMenuId())
                        .roleId(0L)
                        .build();
                list.add(sysMenuRole);
            });
            sysMenuRoleDao.insertSysMenuRoleBySysMenuRoleList(list);
        }
    }
}










































